package com.example.oasystem.controller.notice;

import com.example.oasystem.entity.notice.AoaNoticeListOfSun;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;

import com.example.oasystem.service.AoaNoticeListService;
import com.example.oasystem.service.AoaUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AoaNoticeListController {

    @Autowired
    private AoaNoticeListService aoaNoticeListService;

    @Autowired
    private AoaUserService aoaUserService;


    @RequestMapping("/infrommanage")
    private String selectNoticeAndUserAndTypeAndStatus(Model model,HttpServletRequest request,
                                                       @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                       @RequestParam(value = "baseKey", defaultValue = "") String baseKey){

        AoaUser aoauser = (AoaUser)request.getSession().getAttribute("user");

        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaNoticeListOfSun> noticeList=aoaNoticeListService.selectNoticeAndUserAndTypeAndStatus(pageNum,pageSize,baseKey,aoauser);
        Page page=new Page(noticeList.getSize(), Math.toIntExact(noticeList.getTotal()),noticeList.getPages(),noticeList.getPageNum(),noticeList.isIsFirstPage(),noticeList.isIsLastPage());
        model.addAttribute("list",noticeList.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","noticepaging");
        return "inform/informmanage";
    }

    @RequestMapping("/noticepaging")
    private String noticepaging(Model model, HttpServletRequest request,
                                @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(value = "baseKey", defaultValue = "") String baseKey){

        AoaUser aoauser = (AoaUser) request.getSession().getAttribute("user");
        System.out.println("aoauser34222222222222222222222222222 = " + aoauser);
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaNoticeListOfSun> noticeList=aoaNoticeListService.selectNoticeAndUserAndTypeAndStatus(pageNum,pageSize,baseKey,aoauser);
        Page page=new Page(noticeList.getSize(), Math.toIntExact(noticeList.getTotal()),noticeList.getPages(),noticeList.getPageNum(),noticeList.isIsFirstPage(),noticeList.isIsLastPage());
        model.addAttribute("list",noticeList.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","noticepaging");
        return "inform/informtable";
    }



    @RequestMapping("/informedit")
    public String judgeNotice(@RequestParam(value = "id", required = false,defaultValue = "0") int id,
                              @RequestParam(value = "page", required = false,defaultValue = "1") int pageNum,
                              Model model){
        if (id==0){
            List<AoaTypeList> aoaTypeLists = aoaNoticeListService.selectNoticeType();
            List<AoaStatusList> statusLists = aoaNoticeListService.selectNoticeStatus();
            model.addAttribute("typeList",aoaTypeLists);
            model.addAttribute("statusList",statusLists);
            return "inform/informedit";
        }else {
            AoaNoticeListOfSun noticeList = aoaNoticeListService.selectONENoticeAll(id);
            List<AoaTypeList> aoaTypeLists = aoaNoticeListService.selectNoticeType();
            List<AoaStatusList> statusLists = aoaNoticeListService.selectNoticeStatus();
            model.addAttribute("typeList",aoaTypeLists);
            model.addAttribute("statusList",statusLists);
            model.addAttribute("noticeList",noticeList);
            model.addAttribute("pageNum",pageNum);
            return "inform/informedit";
        }

    }


/*    @RequestMapping("/informcheck")
    public String addNotice(AoaNoticeListOfSun aoaNotice, HttpServletRequest request,
                            @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                            Model model){
        AoaUser aoaUser = (AoaUser)request.getSession().getAttribute("user");
        System.out.println("aoaUser3444444444444444444444444444444 = " + aoaUser);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        aoaNotice.setModifyTime(format.format(date));
        aoaNotice.setUserId(aoaUser.getUserId());
        long noticeId = aoaNotice.getNoticeId();
        if (noticeId!=0){
            System.out.println("修改。。。。。。。。。。。。。。。。。。");
            aoaNoticeListService.updateNotice(aoaNotice);
            model.addAttribute("pageNum",pageNum);
        }else {
            System.out.println("添加。。。。。。。。。。。。。。。。。。");
            if (aoaNotice.getUrl()==null){
                aoaNotice.setUrl("");
            }
            aoaNotice.setNoticeTime(format.format(date));
            List<AoaUser> aoaUsers = aoaUserService.subordinateUsers(aoaUser);
            System.out.println("aoaUsers 88888888888888888= " + aoaUsers);
            aoaNoticeListService.addNotice(aoaNotice,aoaUsers);

        }
        return "forward:/infrommanage";
    }*/
    @RequestMapping("/informcheck")
    public String addNotice(AoaNoticeListOfSun aoaNotice,HttpServletRequest request,
                            @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                            Model model){
        AoaUser aoaUser = (AoaUser)request.getSession().getAttribute("user");
        System.out.println("aoaUser3444444444444444444444444444444 = " + aoaUser);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        aoaNotice.setModifyTime(format.format(date));
        aoaNotice.setNoticeTime(format.format(date));
        aoaNotice.setUserId(aoaUser.getUserId());
        System.out.println("aoaNotice 555555555555555555= " + aoaNotice);

        if (aoaNotice.getNoticeId()!=0){
            System.out.println("修改。。。。。。。。。。。。。。。。。。");
            aoaNoticeListService.updateNotice(aoaNotice);
            model.addAttribute("pageNum",pageNum);
        }else {
            System.out.println("添加。。。。。。。。。。。。。。。。。。");
            if (aoaNotice.getUrl()==null){
                aoaNotice.setUrl("");
            }
            List<AoaUser> aoaUsers = aoaUserService.subordinateUsers(aoaUser);
            System.out.println("aoaUsers 88888888888888888= " + aoaUsers);
            aoaNoticeListService.addNotice(aoaNotice,aoaUsers);
            model.addAttribute("pageNum",pageNum);
        }
        return "forward:/infrommanage";
    }

    @RequestMapping("/informshowOfSun")
    public String updateNotice(@RequestParam("id")long id,Model model){
        AoaNoticeListOfSun notice = aoaNoticeListService.selectUserNotice(id);
        model.addAttribute("notice",notice);
        return "inform/informshowOfSun";
    }

    @RequestMapping("/infromdelete")
    public String delectNotice(@RequestParam("id")long id){
        aoaNoticeListService.delectNotice(id);
        return "forward:/noticepaging";
    }
//
//    @RequestMapping("/noticeTop")
//    public String updateTop(Model model,
//            @RequestParam(value = "isTop", required = false,defaultValue = "0") int isTop,
//            @RequestParam(value = "noticeId", required = false,defaultValue = "0") long noticeId,
//            @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum
//
//    ){
//        model.addAttribute("pageNum",pageNum);
//        aoaNoticeListService.updateTop(isTop,noticeId);
//        return "forward:/infrommanage";
//    }

}
